import fetch from "@/api"
import { ucenterUserGetPublicKey } from "@/api/ucenter/user"
import JSEncrypt from "jsencrypt"
function publicEncrypt(str, pubKey) {
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(pubKey)
  return encrypt.encrypt(str)
}
function ucenterLogin(msg: object): Promise<any> {
  const params = { ...msg }
  return new Promise(async (resolve) => {
    if (window.GLOBALADDRESS.special) {
      // 登陆是否加密
      const res = await ucenterUserGetPublicKey({})
      const pubKey = res?.data
      Object.keys(params).forEach((item) => {
        params[item] = publicEncrypt(params[item], pubKey)
      })
    }
    resolve(
      fetch({
        method: "POST",
        url: "ucenter/login",
        data: params,
        formdata: true,
      })
    )
  })
}

function mockRequest1(params: object): Promise<any> {
  return fetch({
    method: "get",
    url: "ucenter/user/getLoginUser",
    params,
  })
}

export { ucenterLogin, mockRequest1 }
